package net.pimpas.web.jsf.renderers.simpletag.html;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import net.pimpas.web.jsf.components.simpletag.UISimpleTag;
import net.pimpas.web.jsf.util.ComponentUtils;

public class HtmlSimpleTagRenderer extends Renderer {

	@Override
	public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
		if(!component.isRendered()) return;
		if(!ComponentUtils.isCorrectComponentType(component, UISimpleTag.class)) return;
		if(component.getId() == null || component.getId().trim().equals(""))
			component.setId(context.getViewRoot().createUniqueId());
		UISimpleTag simpleTag = (UISimpleTag) component;
		ResponseWriter writer = context.getResponseWriter();
		writer.startElement(simpleTag.getTag(), component);
	}
	
	@Override
	public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
		if(!component.isRendered()) return;
		if(!ComponentUtils.isCorrectComponentType(component, UISimpleTag.class)) return;
		UISimpleTag simpleTag = (UISimpleTag) component;
		ResponseWriter writer = context.getResponseWriter();
		writer.endElement(simpleTag.getTag());
	}
}